package com.netflexitysolutions.amazonws.sdb.orm.internal.operations;

import java.util.concurrent.Callable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.netflexitysolutions.amazonws.sdb.orm.SimpleDBSession;
import com.netflexitysolutions.amazonws.sdb.orm.metadata.ItemMetadata;

public class LoadOneToMany<T> implements Callable<Void> {

	private static Log logger = LogFactory.getLog(LoadOneToMany.class.getName());
	
	private SimpleDBSession session;
	private T record;

	/**
	 * @param session
	 * @param record
	 */
	public LoadOneToMany(SimpleDBSession session, T record) {
		assert(record != null);
		
		this.session = session;
		this.record = record;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.util.concurrent.Callable#call()
	 */
	public Void call() throws Exception {
        // Obtain metadata for this record type.
        ItemMetadata itemMetadata = session.getMetadata(record.getClass());
        assert(itemMetadata != null);
        
        logger.debug("Mapping many-to-one for " + itemMetadata.getDomainName());
        SimpleDBItemMapper.populateOneToMany(session, record, itemMetadata, true);
        
        return null;
	}

	/**
	 * @return the session
	 */
	public SimpleDBSession getSession() {
		return session;
	}

	/**
	 * @param session the session to set
	 */
	public void setSession(SimpleDBSession session) {
		this.session = session;
	}
}
